package com.hd.boot0407.entity;

import cn.hutool.crypto.digest.DigestUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String password;
    private String sex;
    private Date birthday;
    private Date registTime;
    private String auth;
    // 添加字段status，用于记录该用户是否被点过名，初始值0代表没被点过
    private String status;
    // 添加账户余额字段
    private Integer amount;
    private String filename;

    public User(Integer id, String name, String password, String sex, Date birthday, Date registTime) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.sex = sex;
        this.birthday = birthday;
        this.registTime = registTime;
    }

    public User(String name, String password, String sex, Date birthday, Date registTime) {

        this.name = name;
        this.password = password;
        this.sex = sex;
        this.birthday = birthday;
        this.registTime = registTime;
    }
    public static User creatUserfromCsvString(String line) throws Exception {
        String [] split=line.split(",");
        /*密码加密*/
        String md5password = DigestUtil.md5Hex(split[2]);
        /*生日转化*/
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date birthday=sdf.parse(split[4]);
        Date registTime=sdf.parse(split[5]);

        User user=new User(null,split[1],md5password,split[3],birthday,registTime,split[6],split[7],Integer.getInteger(split[8]),split[9]);
        return user;
    }

    public String toCSVString() {
        String s = id +
                "," + name +
                "," + password +
                "," + sex +
                "," + birthday +
                "," + registTime +
                "," + auth +
                "," + status +
                "," + amount +
                "," + filename;
        return s;
    }

}
